package cn.lds.j375.chlm.interceptor;

import cn.lds.j375.chlm.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * 权限拦截器
 */
public class AuthorityInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        User loginUser = (User) session.getAttribute("loginUser");

        Integer roleId = loginUser.getRoleId();
        //如果角色Id不等于1，也就是不是管理员，跳转到错误页面
        if (!roleId.equals(1)){
            request.setAttribute("ex","您没有权限访问");
            request.getRequestDispatcher("/WEB-INF/views/error.jsp").forward(request,response);
        }


        return true;
    }
}
